<template>
  <div>
    <el-form ref="deptForm" label-width="120px">
      <el-form-item label="部门名称">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <!-- <el-option value="1" :label="小美丽" /> -->
          <el-option
            v-for="item in employees"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
        <el-button size="small" @click="hCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
// code	string	非必须		部门编码，同级部门不可重复
// introduce	string	非必须		介绍
// manager	string	非必须		负责人名称
// name	string	非必须		部门名称
// pid	string	非必须		父级部门ID

import { getEmployees } from '@/api/employees'
import { addDepartment, getDepartmentDetail, updateDepartment } from '@/api/departments'
export default {
  props: {
    id: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      employees: [] // 员工列表
    }
  },
  watch: {
    id: function(newVal, oldVal) {
      console.log(newVal)
      // 发请求做数据回填
      if (this.isEdit) {
        this.loadDepartmentDetail()
      }
    }
    // id: {
    //   handler: function() {

    //   },
    //   deep: true,
    //   immediate: true
    // }
  },
  created() {
    console.log('子组件被创建了...')
    this.loadEmployees()

    // 发请求做数据回填
    if (this.isEdit) {
      this.loadDepartmentDetail()
    }
  },
  methods: {
    async loadDepartmentDetail() {
      console.log('要去请求', this.id, '的详情')
      const res = await getDepartmentDetail(this.id)
      this.form = res.data
    },
    async loadEmployees() {
      const res = await getEmployees()
      console.log(res)
      this.employees = res.data
    },
    async doAdd() {
      const d = { ...this.form, pid: this.id }
      console.log(d)
      await addDepartment(d)
      // this.$emit('自定义事件名')
      this.$emit('success')
    },
    async doEdit() {
      await updateDepartment(this.form)
      // this.$emit('自定义事件名')
      this.$emit('success')
    },
    hSubmit() {
      // if (this.isEdit) {
      //   this.doEdit()
      // } else {
      //   this.doAdd()
      // }

      this.isEdit ? this.doEdit() : this.doAdd()
    },
    hCancel() { }
  }
}
</script>
